Completed
Pull Request — master (#214)
by Sander
04:14
created

angular.service(ꞌSettingsServiceꞌ)   B

Complexity

Conditions 1
Paths 2

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
nc 2
nop 3
dl 0
loc 31
rs 8.8571
1
/**
2
 * Nextcloud - passman
3
 *
4
 * @copyright Copyright (c) 2016, Sander Brand ([email protected])
5
 * @copyright Copyright (c) 2016, Marcos Zuriaga Miguel ([email protected])
6
 * @license GNU AGPL version 3 or any later version
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License as
10
 * published by the Free Software Foundation, either version 3 of the
11
 * License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 *
21
 */
22
23
(function () {
24
	'use strict';
25
26
27
	/**
28
	 * @ngdoc service
29
	 * @name passmanApp.SettingsService
30
	 * @description
31
	 * # SettingsService
32
	 * Service in the passmanApp.
33
	 */
34
	angular.module('passmanApp')
35
		.service('SettingsService', ['localStorageService', '$http', '$rootScope', function (localStorageService, $http, $rootScope) {
36
			var settings = {
37
				defaultVault: null,
38
				defaultVaultPass: null
39
			};
40
41
			$http.get(OC.generateUrl('apps/passman/api/v2/settings')).then(function (response) {
42
				if (response.data) {
43
					settings = angular.merge(settings, response.data);
44
					$rootScope.$broadcast('settings_loaded');
45
				}
46
			});
47
48
			var cookie = localStorageService.get('settings');
49
			settings = angular.merge(settings, cookie);
50
			return {
51
				getSettings: function () {
52
					return settings;
53
				},
54
				getSetting: function (name) {
55
					return settings[name];
56
				},
57
				setSetting: function (name, value) {
58
					settings[name] = value;
59
					localStorageService.set('settings', settings);
60
				},
61
				isEnabled: function (name) {
62
					return settings[name] === 1 || settings[name] === '1';
63
				}
64
			};
65
		}]);
66
}());